package com.dss.dsc.serlvet;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;
import org.eclipse.jetty.websocket.WebSocket.Connection;
import org.json.JSONException;
import org.json.JSONObject;

import com.dss.dsc.server.WebAppServer;

public class ExportSlideServlet extends WebSocketServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ExportSlideServlet() {
		// TODO Auto-generated constructor stub
	}

	public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
		// TODO Auto-generated method stub
		return new TailorSocket();
	}

	class TailorSocket implements WebSocket.OnTextMessage {
		private Connection connection;
		public void onClose(int arg0, String arg1) {
			// TODO Auto-generated method stub

		}

		public void onOpen(Connection arg0) {
			// TODO Auto-generated method stub
			this.connection = arg0;
			this.connection.setMaxTextMessageSize(5000000);
		}

		public void onMessage(String arg0) {
			// TODO Auto-generated method stub
			try {
				writeHTML(arg0);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public void writeHTML(String content) throws JSONException,
				IOException {
			System.out.println(content);
			StringBuffer html = new StringBuffer();
			 JSONObject json = new JSONObject(content);
			html.append("<!DOCTYPE HTML>");
			html.append("<HTML>");
			html.append("<HEAD>");
			html.append("<TITLE> "+json.getString("presenterName")+" </TITLE>");
			html.append("<link href=\"../themes/metro-blue/easyui.css\" rel=\"stylesheet\" type=\"text/css\" />");
			html.append("<link href=\"../themes/icon.css\" rel=\"stylesheet\" type=\"text/css\" />");
			html.append("</HEAD>");
			html.append("<BODY class=\"easyui-layout\">");
			html.append("<div id=\"canvas\" data-options=\"region:'center',title:''\">");
			html.append("<div id=\"window\"></div>");
			html.append("<div class=\"easyui-tabs\" id=\"canvasTabs\" data-options=\"fit:true,border:false,plain:true\"></div>");
			html.append("</div></BODY>");
			html.append("<script src=\"../js/load.js\" language=\"javascript\" ></script>");
			html.append("<script>");
			html.append("var content = '" + content + "';");
			html.append("createEditor();");
			html.append("slideViewOP.openSlide(content);");
			html.append("</script></HTML>");

			String path = WebAppServer.readIni("rsc.ini")
					+ "WebRoot/slides/" + json.getString("presenterName") + ".html";
			File file = new File(path);
			if (file.exists()) {
				file.delete();
			}
			FileWriter writer = new FileWriter(path, true);
			writer.write(html.toString());
			writer.close();
		}
	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String content = "aaaaa";
		StringBuffer html = new StringBuffer();
		// JSONObject json = new JSONObject(content);
		html.append("<!DOCTYPE HTML>");
		html.append("<HTML>");
		html.append("<HEAD>");
		html.append("<TITLE> SlideTest </TITLE>");
		html.append("<link href=\"../themes/metro-blue/easyui.css\" rel=\"stylesheet\" type=\"text/css\" />");
		html.append("<link href=\"../themes/icon.css\" rel=\"stylesheet\" type=\"text/css\" />");
		html.append("</HEAD>");
		html.append("<BODY class=\"easyui-layout\">");
		html.append("<div id=\"canvas\" data-options=\"region:'center',title:''\">");
		html.append("<div id=\"window\"></div>");
		html.append("<div class=\"easyui-tabs\" id=\"canvasTabs\" data-options=\"fit:true,border:false,plain:true\"></div>");
		html.append("</div></BODY>");
		html.append("<script src=\"../js/load.js\" language=\"javascript\" ></script>");
		html.append("<script>");
		html.append("var content = " + content + ";");
		html.append("createEditor();");
		html.append("slideViewOP.openSlide(content);");
		html.append("</script></HTML>");

		String path = WebAppServer.readIni("rsc.ini")
				+ "WebRoot/slides/" + /*
											 * json.getString("presenterName"
											 * ) +
											 */"slide.html";
		System.out.println(path);
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		file.createNewFile();
		FileWriter writer = new FileWriter(path, true);
		writer.write(html.toString());
		writer.close();
	}

}
